var async = require('async')

module.exports = function(app) {
  return new Handler(app)
}

var Handler = function(app) {
  this.app = app
  Handler.app = app
  this.sessionService = this.app.get('sessionService')
  this.channelService = this.app.get('channelService')
}

var handler = Handler.prototype
var local = {}

//获取验证码
handler.sendCaptcha = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	var phone = msg.phone
	console.log("phone : "+phone)
	this.app.rpc.game.smsServer.sendCaptcha(null,uid,phone,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//绑定手机号
handler.bindPhone = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	var code = msg.code  
	this.app.rpc.game.smsServer.bindPhone(null,uid,code,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//获取绑定手机信息
handler.getPhoneInfo = function(msg,session,next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,data : "已断开连接"})
		return
	}
	this.app.rpc.db.remote.getPhoneInfo(null,uid,function(data) {
		next(null,{flag : true,data : data})
	})
}


//发送登陆验证码
handler.sendLoginCaptcha = function(msg,session,next) {
	var phone = msg.phone
	this.app.rpc.game.smsServer.sendLoginCaptcha(null,phone,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}

//提交登陆验证码
handler.bindToken = function(msg,session,next) {
	var phone = msg.phone
	var code = msg.code
	this.app.rpc.game.smsServer.bindToken(null,phone,code,function(flag,data) {
		next(null,{flag : flag,data : data})
	})
}